#include <iostream>
#include <unistd.h>
#include <signal.h>

void PendingPrint(sigset_t *pending)
{
    std::cout << "current proccess sig[" << getpid() << "]: ";
    for (int i = 31; i >= 1; i--)
    {
        if (sigismember(pending, i))
        {
            std::cout << 1;
        }
        else
            std::cout << 0;
    }
    std::cout << '\n';
}
void handler(int sig)
{
    std::cout << sig << "号信号已递达！" << std::endl;
}
void WorkSig(sigset_t* bites,sigset_t* oldset,int retrycnt)
{
    while (true)
    {
        sigset_t pending;
        sigpending(&pending);
        PendingPrint(&pending);
        if (retrycnt == 0 || retrycnt == -5)
        {
            sigprocmask(SIG_SETMASK, oldset, bites);
            oldset = bites;
            retrycnt = retrycnt == -5 ? 15 :0;
        }
        --retrycnt;
        sleep(1);
    }
}
int main()
{
    // 捕捉SIGINT信号
    signal(SIGINT, handler);
    // 屏蔽SIGINT号信号
    sigset_t bites, oldset;
    // 清空信号集
    sigemptyset(&bites);
    sigemptyset(&oldset);
    // 增加信号到栈区的block位图中
    sigaddset(&bites, SIGINT);
    // 修改pcb内核block位图
    sigprocmask(SIG_BLOCK, &bites, &oldset);
    // 复原计时器
    int retrycnt = 15;
    WorkSig(&bites,&oldset,retrycnt);
    return 0;
}